import {
  getTokenMsgArr
} from '@/config/apiServe/fetch-config.js'
import {
  filterAdrTag
} from "@/utils/filters.js";
import QRCode from 'qrcodejs2'

export default {
  data() {
    return {
      code: {},
      baseCoin: require('../../assets/img/coin/baseCoin.png'),
      adrInfo: {
        adr: '',
        balance: 0,
        reciver: 0,
        txCount: 0
      },
      contractInfo: [],
      contractSearch: '',
      totalValue: 0,
      tokenBalance: [],
      url: ''
    }
  },
  watch: {
    $route() {
      this.adrInfo = {
        adr: '',
        balance: 0,
        reciver: 0,
        txCount: 0
      }
      this.tokenBalance = []
      this.totalValue = 0
      this.init()
      this.makeCode(this.$route.query.address)
    }
  },
  mounted() {
    this.init()
    this.drawCode(this.$route.query.address)
  },
  methods: {
    init() {
      if (this.$route.query.address) {
        this.adrInfo.adr = this.$route.query.address
        this.getAdrInfo(this.adrInfo.adr)
        this.getTokenAssets(this.adrInfo.adr)
      } else {
        this.$notify({
          title: this.$t('components.tip'),
          message: this.$t('components.errParam'),
          type: "warning",
          duration: 1000
        });
        this.$router.push({
          path: "/"
        });
      }
    },
    isContact(adr) {
      return filterAdrTag(adr);
    },
    getAdrInfo(adr) {
      this.Rpc.getAddrOverview(adr).then(data => {
        if (data.error === null)
          this.adrInfo = {
            ...this.adrInfo,
            ...data.result
          }
      })
      // 获取地址其他合约下的余额
      this.Rpc.getAllExecBalance(this.adrInfo.adr).then(data => {
        if (data.error === null) {
          this.contractInfo = data.result.execAccount || []
          // if (this.contractInfo) {
          //   this.contractInfo.forEach(item => {
          //     if (item.execer === 'coins') {
          //       this.adrInfo.balance = item.account.balance || 0
          //       this.adrInfo.frozen = item.account.frozen || 0
          //     }
          //   })
          // }
        }
      })
    },
    // 查看指定合约下资产信息
    searchContract(ticket) {
      this.contractInfo.forEach(item => {
        if (item.execer === ticket) {
          this.adrInfo.balance = item.account.balance || 0
          this.adrInfo.frozen = item.account.frozen || 0
        }
      })
    },
    // 获取地址下Token余额
    getTokenAssets(adr) {
      let infos = []
      let tokens = []
      this.Rpc.getAddrTokenAssets(adr, 'token').then(data => {
        if (data.error === null) {
          data.result.tokenAssets.map(current => {
            if (current.account.balance > 0) {
              tokens.push(current.symbol)
              infos.push({
                balance: current.account.balance / 100000000,
                symbol: current.symbol,
                rmb: 0,
                icon: this.baseCoin,
                nickname: ''
              })
            }
          })
          // this.tokenBalance = infos
          getTokenMsgArr(tokens).then(res => {
            res.data.map(current => {
              for (let i = 0; i < infos.length; i++) {
                if (current.name === infos[i].symbol) {
                  infos[i].icon = current.icon || this.baseCoin
                  infos[i].rmb = current.rmb || 0
                  infos[i].nickname = current.nickname
                }
              }
            })
            infos.map(item => {
              this.totalValue += item.balance * item.rmb
            })
            this.tokenBalance = infos
          })
        } else return
      })
    },
    drawCode(addr) {
      this.code = new QRCode('qrcode', {
        width: 100,
        height: 100,
        text: addr
      })
      console.log(this.code)
    },
    makeCode(addr) {
      this.code.makeCode(addr)
    },
    downCode() {
      let imgs = document.getElementById("qrcode").getElementsByTagName("img")
      let a = document.createElement('a')
      a.download = `code${new Date().getTime()}.png`
      a.href = imgs[0].src
      this.url = document.getElementById("qrcode").getElementsByTagName("img")[0].src
      document.body.appendChild(a);
      a.click();
      document.body.removeChild(a);
    },
    success() {
      this.$message.success(this.$t('components.copySuc'));
    },
    error() {
      this.$message.warning(this.$t('components.copyErr'));
    }
  }
}